package com.uit.httt04.duongvanlam.facedetection.utils;

import com.uit.httt04.duongvanlam.facedetection.classes.ScoreAlgorithm;

import android.util.Log;

public class Gaussian
{
	private String TAG = "Gaussian";

	private double mean;
	private double var;

	public Gaussian()
	{
		this.mean = 0.0;
		this.var = 0.0;
	}

	public void calculateMeanAndVar(ScoreAlgorithm[] input) throws Exception
	{
		if (input != null && input.length > 0)
		{
			this.mean = 0.0;
			this.var = 0.0;
			//
			//
			double sumOfElements = 0.0;
			for (int i = 0; i < input.length; i++)
			{
				sumOfElements = sumOfElements + input[i].getScore();
			}

			this.mean = sumOfElements / (double) input.length;

			for (int i = 0; i < input.length; i++)
			{
				this.var = this.var + Math.pow(input[i].getScore() - this.mean, 2);
			}

			this.var = this.var / (double) (input.length - 1);
			this.var = Math.sqrt(this.var);

			Log.d(TAG, "Mean: " + String.valueOf(this.mean));
			Log.d(TAG, "Var: " + String.valueOf(this.var));
		}
		else
			throw new Exception("Input is null");
	}

	public double calculateGaussian(ScoreAlgorithm input) throws Exception
	{
		return Math.pow(Math.E, -((Math.pow(input.getScore() - this.mean, 2)) / (2 * Math.pow(this.var, 2)))) / this.var * Math.sqrt(2 * Math.PI);
	}
}
